# ...existing code...
import sys
from pathlib import Path

# Ensure project root (server/) is on sys.path so `app` package can be imported
root = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(root))

from fastapi.testclient import TestClient
import importlib

# Import the database module and create tables for the currently configured DATABASE_URL
if 'app.models.database' in sys.modules:
	importlib.reload(sys.modules['app.models.database'])
else:
	importlib.import_module('app.models.database')

from app.models.database import Base, engine
Base.metadata.create_all(bind=engine)

from app.main import app

# Use TestClient (this will trigger startup/shutdown handlers too)
client = TestClient(app)

print('Creating item via POST /api/items')
res = client.post('/api/items', json={'title':'smoke','description':'smoke test'})
print('POST status:', res.status_code, 'body:', res.json())
item = res.json()
item_id = item.get('id')

print('Reading item via GET /api/items/{id}')
res2 = client.get(f'/api/items/{item_id}')
print('GET status:', res2.status_code, 'body:', res2.json())

print('Listing items')
res3 = client.get('/api/items')
print('LIST status:', res3.status_code, 'count:', len(res3.json()))

print('Smoke test complete')
